package jjn.carl.backtrack;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Jiang Jining
 * @since 2023-07-23 21:40
 */
public class LeetCode77 {
    public List<List<Integer>> combine(int n, int k) {
        List<List<Integer>> result = new ArrayList<>();
        backtrack(1, n, k, result, new ArrayList<>());
        return result;
    }
    
    private void backtrack(int startIndex, int n, int k, List<List<Integer>> result, List<Integer> path) {
        if (path.size() == k) {
            result.add(new ArrayList<>(path));
            return;
        }
        for (int i = startIndex; i <= n - (k - path.size()) + 1; i++) {
            path.add(i);
            backtrack(i + 1, n, k, result, path);
            path.remove(path.size() - 1);
        }
    }
    
    public static void main(String[] args) {
        System.out.println("new LeetCode77().combine(4,2) = " + new LeetCode77().combine(4, 2));
    }
}
